package phoneDataComparable;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.compress.BZip2Codec;
import org.apache.hadoop.io.compress.CompressionCodec;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

/**
 * 驱动类
 * @author  qiang.ye
 */
public class FlowCountDriver {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        long t1 = System.currentTimeMillis();
        args = new String[]{"D:\\data\\demodatas\\phone1\\","D:\\data\\demodatas\\phone21"};

        //1.获取配置信息
        Configuration conf = new Configuration();
        //开启map端压缩
        conf.setBoolean("mapreduce.map.out.compress.codec",true);
        //设置map端输出压缩方式
        conf.setClass("mapreduce.map.output.compress.codec", BZip2Codec.class, CompressionCodec.class);
        Job job = Job.getInstance(conf);

        //2.找反射的3个类
        job.setJarByClass(FlowCountDriver.class);
        job.setMapperClass(FlowCountMapper.class);
        job.setReducerClass(FlowCountReducer.class);


        //map输出的k,v（或者说reduce的输入的K,V）
        job.setMapOutputKeyClass(FlowBean.class);
        job.setMapOutputValueClass(Text.class);
        //reduce输出的K，V
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(FlowBean.class);

        //设置reduce端输出压缩开启
        FileOutputFormat.setCompressOutput(job,true);
        //设置压缩的方式
        FileOutputFormat.setOutputCompressorClass(job,BZip2Codec.class);


        //设置combiner
//        job.setCombinerClass(WordCountCombiner.class);

        //设置分区
//        job.setPartitionerClass(WordCountPartitioner.class);
//        job.setNumReduceTasks(2);

        //
//        job.setInputFormatClass(CombineTextInputFormat.class);
//        CombineTextInputFormat.setMaxInputSplitSize(job,3*1024*1024);
//        CombineTextInputFormat.setMinInputSplitSize(job,1*1024*1024);
        //4.数据的来源
        FileInputFormat.setInputPaths(job,new Path(args[0]));
        FileOutputFormat.setOutputPath(job,new Path(args[1]));

        //5.提交job
        boolean result = job.waitForCompletion(true);
        long t2 = System.currentTimeMillis();
        System.exit(result?0:1);
        System.out.println("是否完成:"+result);

        System.out.println("耗时:"+(t2-t1));
    }

}
